﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp7
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“schoolsDataSet1.Students”中。您可以根据需要移动或删除它。
            string cmdString = "select * from Students";

            var dt = DbHelper.GetDataTable(cmdString);

            dataGridView1.DataSource = dt;

            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dataGridView1.AllowUserToAddRows = false;

        }
        //查询
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;

            string cmdString = string.Format("select * from Students where Age like '%{0}%'", name);

            var dt = DbHelper.GetDataTable(cmdString);

            dataGridView1.DataSource = dt;
        }
        //添加
        private void button2_Click(object sender, EventArgs e)
        {
            Form4 form4 = new Form4();

            var res = form4.ShowDialog();

            if (res.Equals(DialogResult.Yes))
            {
                string cmdSting = "select * from Students";

                var data = DbHelper.GetDataTable(cmdSting);

                dataGridView1.DataSource = data;
            }
        }
        //修改
        private void button3_Click(object sender, EventArgs e)
        {

        }
        //删除
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
              

                var test = MessageBox.Show("确定删除!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (test.Equals(DialogResult.Yes))
                {
                    object Id = null;

                    string cmdString = string.Format("delete Students where Id = '{0}'", Id);

                    var list = DbHelper.AddOrEditSave(cmdString);

                    if (list == 1)
                    {
                        MessageBox.Show("删除成功！", "提示！");

                        string select = "select * from Students";

                        var wer = DbHelper.GetDataTable(select);

                        dataGridView1.DataSource = wer;
                    }
                }
                else
                {
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("请选择行！", "提示");
            }
        }
    }
}
